﻿using Aeon.Configuration;
using Aeon.Emulator.Dos.VirtualFileSystem;

namespace Aeon.DiskImages
{
    /// <summary>
    /// CD-ROM ISO image configuration.
    /// </summary>
    [DriveConfiguration("Aeon.ISOImage")]
    public sealed class ISOImageConfiguration : DiskImageConfiguration
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the ISOImageConfiguration class.
        /// </summary>
        public ISOImageConfiguration()
        {
        }
        #endregion

        #region Public Static Properties
        /// <summary>
        /// Gets the name of the configuration type.
        /// </summary>
        public static string Name
        {
            get { return "CD-ROM ISO image file"; }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets a value indicating whether the drive is read-only.
        /// </summary>
        public override bool IsReadOnly
        {
            get { return true; }
        }
        #endregion

        #region Internal Properties
        /// <summary>
        /// Gets the extensions of supported image files.
        /// </summary>
        internal override string FileExtensions
        {
            get { return "*.iso"; }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Returns a new instance of the emulated drive.
        /// </summary>
        /// <returns>New instance of the emulated drive.</returns>
        public override IMappedDrive Create()
        {
            return new ISOImage(this.ImageFilePath);
        }
        /// <summary>
        /// Returns a deep copy of the drive configuration.
        /// </summary>
        /// <returns>Deep copy of the drive configuration.</returns>
        public override IDriveConfiguration Clone()
        {
            return new ISOImageConfiguration() { ImageFilePath = this.ImageFilePath };
        }
        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates the ImageFilePath property.
        /// </summary>
        protected override void ValidateImageFilePath()
        {
            var path = this.ImageFilePath;
            if(string.IsNullOrEmpty(path))
            {
                SetPropertyError("ImageFilePath", "Image file path is required.");
                return;
            }

            if(path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0)
            {
                SetPropertyError("ImageFilePath", "Invalid image file path.");
                return;
            }

            if(!System.IO.File.Exists(path))
            {
                SetPropertyError("ImageFilePath", "Invalid image file path.");
                return;
            }

            //try
            //{
            //    using(var image = System.IO.File.OpenRead(path))
            //    using(var system = new DiscUtils.Iso9660.CDReader(image, false, true))
            //    {
            //    }
            //}
            //catch
            //{
            //    SetPropertyError("ImageFilePath", "Image file must contain a valid ISO9660 file system.");
            //    return;
            //}

            ClearPropertyError("ImageFilePath");
        }
        #endregion
    }
}
